
//////////////////////////////////////////////////////////////////////////////////
//
//	DoD Follow The Wounded
//		- Version 0.2
//		- 07.09.2006
//		- dod port: diamond-optic
//		- original: KRoT@L
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
//   - When the health of a player goes below ftw_health,
//     the footsteps of this player will be covered with blood,
//     thus you can follow him.
//
// CVARs:
//
//   ftw_active //Turn on(1)/off(0)
//   ftw_health //Health needs to be below this for effect
//
// Credits:
//
//   - Original plugin by KRoT@L
//   - http://www.amxmodx.org/forums/viewtopic.php?t=17688
//   - Ported to DoD by diamond-optic
//
// Known Problems:
//
//   - Sometime the footprints show up at wierd angles on the ground.
//
// Changelog:
//
//   - 02.23.2006 Version 0.1
//  	  Initial DoD port release
//
//   - 07.09.2006 Version 0.2
//  	  Replaced ENGINE module with FAKEMETA
//	  Removed AMXMISC module
//
//////////////////////////////////////////////////////////////////////////////////

#include <amxmodx>
#include <fakemeta>
#include <dodx>

new decals[2] = {120,121}

new bool:g_isDying[33]
new g_decalSwitch[33]

public plugin_init()
{
	register_plugin("DoD Follow the Wounded", "0.2", "AMXX DoD Team & KRoTaL")
	
	register_cvar("dod_follow_the_wounded_stats", "0.2", FCVAR_SERVER|FCVAR_SPONLY)

	register_cvar("ftw_active", "1")
	register_cvar("ftw_health", "5")

	register_event("ResetHUD","resethud_event","be")
	
	register_statsfwd(XMF_DAMAGE)
	register_statsfwd(XMF_DEATH)
}

public client_connect(id)
{
	g_isDying[id] = false
	remove_task(4247545+id)
}

public resethud_event(id)
{
	if(g_isDying[id])
	{
		g_isDying[id] = false
		remove_task(4247545+id)
	}
}

public make_footsteps(param[])
{
	new id = param[0]
	if(!is_user_alive(id) || get_cvar_num("ftw_active") == 0 || get_user_speed(id) < 120) return
	new origin[3]
	get_user_origin(id, origin)
	
	if(pev(id, pev_bInDuck) == 1)
		origin[2] -= 18
	else
		origin[2] -= 36
	new Float:velocity[3]
	new Float:ent_angles[3]
	new Float:ent_origin[3]
	
	new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))

	pev(id, pev_v_angle, ent_angles)
	pev(id, pev_origin, ent_origin)

	if(ent > 0)
	{
		ent_angles[0] = 0.0   //tried changing to 180
		if(g_decalSwitch[id] == 0) ent_angles[1] -= 90
		else ent_angles[1] += 90
		set_pev(ent, pev_origin, ent_origin)
		set_pev(ent, pev_v_angle, ent_angles)
		velocity_by_aim(ent, 12, velocity)

		engfunc(EngFunc_RemoveEntity, ent)
	}
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY, origin)
	write_byte(116)
	write_coord(origin[0] + floatround(velocity[0]))
	write_coord(origin[1] + floatround(velocity[1]))
	write_coord(origin[2])
	write_byte(decals[g_decalSwitch[id]])
	message_end()
	g_decalSwitch[id] = 1 - g_decalSwitch[id]
	return
}

public client_damage(attacker,victim,damage,wpnindex,hitplace,TA)
{
	if(get_cvar_num("ftw_active") == 0) return PLUGIN_CONTINUE

	if(!g_isDying[victim] && is_user_alive(victim) && get_user_health(victim) <= get_cvar_num("ftw_health"))
	{
		g_isDying[victim] = true
		g_decalSwitch[victim] = 0
		new param[1]
		param[0] = victim
		set_task(0.2, "make_footsteps", 4247545+victim, param, 1, "b")
	}
	return PLUGIN_CONTINUE
}

public client_death(killer,victim,wpnindex,hitplace,TK)
{
	if(g_isDying[victim])
	{
		g_isDying[victim] = false
		remove_task(4247545+victim)
	}
}

stock get_user_speed(id)	//stock from fakemeta_util by VEN
{		
	new Float:Vel[3]
	pev(id, pev_velocity, Vel)

	return floatround(vector_length(Vel))
}
